package GameBoard;

import java.util.Vector;

import GameElements.Pawn;
import GameElements.PawnFactory;
import GameElements.Tile;
import GameElements.TileAubCath;
import GameElements.TileFactory;
import GameElements.Elements.Element;
import Player.Player;
import Utils.Point3D;

public class BasicGameBoard extends GameBoard 
{
	public BasicGameBoard()
	{
		initTable();
		initStock();
		melangerStock();
	}
	
	protected void initTable()
	{
		String path = "img/tiles/";
		Tile t = TileFactory.getInstance().createTile(path, "4.png", 1).get(0);
		if(t!=null)
			table[width/2][height/2] = t;

	}
		
	protected void initStock()
	{	
		String path = "img/tiles/";
		stockAdd(TileFactory.getInstance().createTile(path, "1.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "2.png", 4));
		stockAdd(TileFactory.getInstance().createTile(path, "3.png", 1));
		stockAdd(TileFactory.getInstance().createTile(path, "4.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "5.png", 5));
		stockAdd(TileFactory.getInstance().createTile(path, "6.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "7.png", 1));
		stockAdd(TileFactory.getInstance().createTile(path, "8.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "9.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "10.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "11.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "12.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "13.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "14.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "15.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "16.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "17.png", 1));
		stockAdd(TileFactory.getInstance().createTile(path, "18.png", 3));
		stockAdd(TileFactory.getInstance().createTile(path, "19.png", 2));
		stockAdd(TileFactory.getInstance().createTile(path, "20.png", 1));
		stockAdd(TileFactory.getInstance().createTile(path, "21.png", 8));
		stockAdd(TileFactory.getInstance().createTile(path, "22.png", 9));
		stockAdd(TileFactory.getInstance().createTile(path, "23.png", 4));
		stockAdd(TileFactory.getInstance().createTile(path, "24.png", 1));
	}


	public void distributePawn(Vector<Player> player)
	{
		for(int i=0; i<player.size(); i++)
			player.get(i).addPawn(PawnFactory.getInstance().createPawn("basic", i, 8));
	}

	public boolean canAdd(Tile t, int i, int j)
	{
		if(i >= width - 1 || j >= height - 1 || i <= 1 || j <= 1 )
			return false;
		
		//la place n'est pas occupé
		if(table[i][j] != null)
			return false;
		
		Tile tN = table[i][j-1];
		Tile tE = table[i+1][j];
		Tile tS = table[i][j+1]; 
		Tile tW = table[i-1][j];
		
		//au moin un voisin
		if(!(tN!=null || tE!=null || tS!=null || tW!=null))
			return false;
		
		//test nord
		if(tN != null)
			if(t.getNorth() != tN.getSouth())
				return false;
		//test est
		if(tE != null)
			if(t.getEast() != tE.getWest())
				return false;
		//test sud
		if(tS != null)
			if(t.getSouth() != tS.getNorth())
				return false;
		//test ouest
		if(tW != null)
			if(t.getWest() != tW.getEast())
				return false;
			
		return true;
	}
	
	public boolean canAdd(Pawn pawn, int i, int j, int indexPawnInTile)
	{
		Tile t = table[i][j];
		if(t==null)
			return false;
		
		int adaptedIndex = t.findAdaptedIndex(indexPawnInTile);
			
		if(adaptedIndex == Tile.c)
		{	
			if(t.getCenter() == Tile.cloister)
				return true; //add the pawn to the cloister(in the center of the tile)
			else
				return false; //sinon refuser d'ajouter au centre
		}
		else if(adaptedIndex==Tile.n1 || adaptedIndex==Tile.e1 || adaptedIndex==Tile.s1 || adaptedIndex==Tile.w1)
		{
			Vector<Point3D> chemin = new Vector<Point3D>();
			return algoStreetCastle(i, j, adaptedIndex, chemin);
		}
		else
		{
			Vector<Point3D> chemin = new Vector<Point3D>();
			return algoStreetCastle(i, j, adaptedIndex, chemin);//mettre l'autre algo apres
		}
	}

	@Override
	public void majScoreFinal() 
	{
		//System.out.println("majScoreFinal a faire");	
	}

	@Override
	public void majScoreTour() 
	{
		for (int i=0; i<elementsCompleted.size(); i++) 
		{
			Element elt = elementsCompleted.get(i);
			//mettre a jour le score des joueurs
			scoreJoueurs(elt.getScoreDonneInComplete(), elt.getChemin());
			//les joueurs recuperent leurs pions
			for(int j=0; j<elt.getEnsTileContentPawn(table).size(); j++)
			{
				Tile t = elt.getEnsTileContentPawn(table).get(j);
				Pawn pion = t.getPionPose(); 
				pion.getJoueur().recupPion(pion, t);
			}
		}
	}

	@Override
	public void scoreJoueurs(int score, Vector<Point3D> chemin) 
	{
		int[] nb = new int[5];
		Vector<Pawn> pawns = getPawnInChemin(chemin);
		Vector<Player> player = getPlayer(pawns);
		for(int i=0; i<pawns.size(); i++)
		{
			int id = pawns.get(i).getJoueur().getId();
			nb[id]++;
		}
		Vector<Integer> iMax = Imax(nb);
		for(int i=0; i<iMax.size(); i++)
		{
			for(int j=0; j<player.size(); j++)
			{
				if(player.get(j).getId() == iMax.get(i))
					player.get(j).addPoint(score);
			}
		}
	}
	
	public Vector<Player> getPlayer(Vector<Pawn> pawns)
	{
		Vector<Player> player = new Vector<Player>();
		for(int i=0; i<pawns.size();i++)
		{
			boolean add = true;
			Player p = pawns.get(i).getJoueur(); 
			for(int j=0; j<player.size(); j++)
			{
				if(p.getId() == player.get(j).getId())
					add = false;					
			}
			if(add)
				player.add(p);
		}
		return player;
	}
	
	public Vector<Integer> Imax(int[] tab)
	{
		Vector<Integer> imax = new Vector<Integer>();
		int max=-1;
		for(int i=0; i<tab.length; i++)
		{
			if(tab[i] > max)
				max = tab[i];
		}
		for(int i=0; i<tab.length; i++)
		{
			if(tab[i]==max)
				imax.add(i);
		}
		return imax;
	}
	
}
